package cn.javaweb.servlet;

import cn.javaweb.entity.User;
import cn.javaweb.library.AjaxResult;
import cn.javaweb.model.UserModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user/logout")
public class LogOutServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try{
            User user = (User) req.getAttribute("user");

            UserModel userModel = new UserModel();

            userModel.update("update user set token='' where id=" + user.getId(), null);

            resp.getWriter().print(AjaxResult.success("退出成功"));
        }catch (Exception e){
            resp.getWriter().print(AjaxResult.error("操作失败"));
        }
    }
}
